mini.parse();

function GoodsFormHandler() {
};

var dynamicIdsArray = new Array();
var staticIdsStr = "";
var shopTypeAttribute = "";
var counts = 0;
var gBgiId;             // 全局的商品编号
var gBggId;             // 全局的商品分类编号
var gBshoptype;         // 全局的商品类型编号
var GOODSATTR = [];     // 存储所有属性的对象数组
var gbGoodsType = "";   // 全局的商品类型编号，与上面定义的使用不同。

GoodsFormHandler.instance = new GoodsFormHandler();

// 获得全局的商品数据表格对象
GoodsFormHandler.goodsForm = new mini.Form("goodsForm");
GoodsFormHandler.formData = {};

// 商品品牌提示选择项
GoodsFormHandler.bgbComboxData = {
	"bgbId" : "-1",
	"bgbName" : "请选择..."
};
// 商品供货商提示选项
GoodsFormHandler.bgsComboxData = {
	"bsiId" : "-1",
	"bsiName" : "请选择..."
};
// 通用提示选项
GoodsFormHandler.commonWarningData = {
	"id" : "-1",
	"text" : "请选择..."
};
// 商品状态数据
GoodsFormHandler.bgiStateData = [{
			text : "有效",
			value : 1
		}, {
			text : "无效",
			value : 0
		}];
mini.get("bgiState").setData(GoodsFormHandler.bgiStateData);

GoodsFormHandler.bgiFeatureId = "bgiFeature_gft";
GoodsFormHandler.bgiDetailId = "bgiDetail_gft";

/**
 * 加载商品品牌下拉组合框
 */
GoodsFormHandler.getBgbCombobox = function() {
	$.ajax({
				url : __root + "/getBgbCombobox.do",
				type : "POST",
				cache : false,
				dataType : 'json',
				success : function(text) {
					var temp = [GoodsFormHandler.bgbComboxData];
					for (var i = 0; i < text.length; i++) {
						var item = {
							bgbId : text[i].bgbId,
							bgbName : text[i].bgbName
						};
						temp[i + 1] = item;
					}

					mini.get("bgiBrand_gft").setData(temp);
				},
				error : function(jqXHR, textStatus, errorThrown) {
					MiniuiTools.miniError("系统提示", jqXHR.responseText);
				}
			});
}

/**
 * 加载供货商combobox
 */
GoodsFormHandler.getBgsCombobox = function() {
	$.ajax({
				url : __root + "/getBgsCombobox.do",
				type : "POST",
				cache : false,
				dataType : 'json',
				success : function(text) {
					var temp = [GoodsFormHandler.bgsComboxData];
					for (var i = 0; i < text.length; i++) {
						var item = {
							bsiId : text[i].bsiId,
							bsiName : text[i].bsiName
						};
						temp[i + 1] = item;
					}
					mini.get("bgiSupplier_gft").setData(temp);
				},
				error : function(jqXHR, textStatus, errorThrown) {
					MiniuiTools.miniError("系统提示", jqXHR.responseText);
				}
			});
}

GoodsFormHandler.getBgbCombobox();
GoodsFormHandler.getBgsCombobox();

try {
	var bgiFeatureEditor = KindEditor.create('#' + GoodsFormHandler.bgiFeatureId, {
				resizeType : 0,
				uploadJson : "../../resource/kindeditor/jsp/upload_json.jsp?imgDir=" + __proImgPath,
				fileManagerJson : '../../resource/kindeditor/jsp/file_manager_json.jsp',
				allowPreviewEmoticons : false,
				allowImageUpload : true,
				items : ['undo', 'redo', '|', 'fontname', 'fontsize', '|',
						'forecolor', 'hilitecolor', 'bold', 'italic',
						'underline', 'removeformat', '|', 'justifyleft',
						'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image',
						'multiimage', 'link', 'unlink', '|', 'table', 'hr',
						'pagebreak', '|', 'source', 'preview', 'fullscreen']
			});

	var bgiDetailEditor = KindEditor.create('#' + GoodsFormHandler.bgiDetailId,
			{
				resizeType : 0,
				uploadJson : "../../resource/kindeditor/jsp/upload_json.jsp?imgDir=" + __proImgPath,
				fileManagerJson : '../../resource/kindeditor/jsp/file_manager_json.jsp',
				allowPreviewEmoticons : false,
				allowImageUpload : true,
				items : ['undo', 'redo', '|', 'fontname', 'fontsize', '|',
						'forecolor', 'hilitecolor', 'bold', 'italic',
						'underline', 'removeformat', '|', 'justifyleft',
						'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image',
						'multiimage', 'link', 'unlink', '|', 'table', 'hr',
						'pagebreak', '|', 'source', 'preview', 'fullscreen']
			});
} catch (e) {
}

/**
 * 唯一属性生成表单控件 a：唯一属性 + 文本框 = mini-textbox b：唯一属性 + 文本域 = mini-textarea c：唯一属性 +
 * 从列表中选择 = mini-combobox
 * 
 * @param {}
 *            mode
 * @param {}
 *            id
 * @return {}
 */
GoodsFormHandler.createMode0RHtml = function(mode, id) {
	if (mode == 0) {
		return "<input id=" + id + " name=" + id
				+ " class='mini-textbox' width='140'/>";
	} else if (mode == 1) {
		return "<input id=" + id + " name=" + id
				+ " class='mini-textarea' width='140'/>";
	} else if (mode == 2) {
		return "<input id="
				+ id
				+ " name="
				+ id
				+ " class='mini-combobox' textField='text' valueField='id' width='140' />";
	} else {
		return "<a>属性资源配置有误...</a>";
	}

}

/**
 * 单选属性、复选属性生成表单控件 d：单选属性 + 文本框 = mini-textbox + mini-textbox + mini-textbox
 * e：单选属性 + 文本域 = mini-textarea + mini-textbox + mini-textbox f：单选属性 + 从列表中选择 =
 * mini-textbox + mini-textbox + mini-textbox
 * 
 * g：复选属性 + 文本框 = mini-textbox + mini-textbox + mini-textbox h：复选属性 + 文本域 =
 * mini-textarea + mini-textbox + mini-textbox i：复选属性 + 从列表中选择 = mini-textbox +
 * mini-textbox + mini-textbox
 * 
 * @param {}
 *            mode
 * @param {}
 *            btaId
 * @return {}
 */
GoodsFormHandler.createMode1RHtml = function(mode, btaId) {

	if (mode == 0) {
		return "<input id="
				+ btaId
				+ "0"
				+ " name="
				+ btaId
				+ "0"
				+ " class='mini-textbox' width='140'/>"
				+ "<span style='padding-left:15px;'>属性价格：</span>&nbsp;<input id="
				+ btaId
				+ "1"
				+ " name="
				+ btaId
				+ "1"
				+ " class='mini-textbox' width='140'/>"
				+ "<span style='padding-left:15px;'>属性排序：</span>&nbsp;<input id="
				+ btaId + "2" + " name=" + btaId + "2"
				+ " class='mini-textbox' width='140'/>";

	} else if (mode == 1) {
		return "<input id="
				+ btaId
				+ "0"
				+ " name="
				+ btaId
				+ "0"
				+ " class='mini-textarea' width='140'/>"
				+ "<span style='padding-left:15px;'>属性价格：</span>&nbsp;<input id="
				+ btaId
				+ "1"
				+ " name="
				+ btaId
				+ "1"
				+ " class='mini-textbox' width='140'/>"
				+ "<span style='padding-left:15px;'>属性排序：</span>&nbsp;<input id="
				+ btaId + "2" + " name=" + btaId + "2"
				+ " class='mini-textbox' width='140'/>";
	} else if (mode == 2) {
		return "<input id="
				+ btaId
				+ "0"
				+ " name="
				+ btaId
				+ "0"
				+ " class='mini-combobox' textField='text' valueField='id' width='140'/>"
				+ "<span style='padding-left:15px;'>属性价格：</span>&nbsp;<input id="
				+ btaId
				+ "1"
				+ " name="
				+ btaId
				+ "1"
				+ " class='mini-textbox' width='140'/>"
				+ "<span style='padding-left:15px;'>属性排序：</span>&nbsp;<input id="
				+ btaId + "2" + " name=" + btaId + "2"
				+ " class='mini-textbox' width='140'/>";
	} else {
		return "<a>属性资源配置有误...</a>";
	}
}

/**
 * 左侧单元格HTML
 * 
 * @param {}
 *            aid
 * @param {}
 *            btaName
 * @param {}
 *            btaMode
 * @param {}
 *            cobData
 * @return {}
 */
GoodsFormHandler.createMode1LHtml = function(aid, btaName, btaMode, cobData) {
	return "<a id=" + aid + " style='font-weight:bold;cursor: pointer;'"
			+ "onclick='GoodsFormHandler.addNewRow(\"" + btaName + "\", "
			+ btaMode + ", " + mini.encode(cobData) + ",\"" + aid
			+ "\")'>[+]</a> " + btaName + "：";
}

/**
 * 左侧可删除项HTML
 * 
 * @param {}
 *            aid
 * @param {}
 *            btaName
 * @param {}
 *            btaMode
 * @param {}
 *            cobData
 * @param {}
 *            counts
 * @return {}
 */
GoodsFormHandler.createMode1Reduce = function(aid, btaName, btaMode, cobData,
		counts) {
	var s = aid + "_" + counts;
	return "<a id=" + aid + " style='font-weight:bold;cursor: pointer;'"
			+ "onclick='GoodsFormHandler.delNewRow(\"" + s + "\")'>[—]</a> "
			+ btaName + "：";
}

/**
 * 生成属性列表表格
 * 
 * @param {}
 *            $tab_td_left
 * @param {}
 *            tab_td_left_html
 * @param {}
 *            $tab_td_right
 * @param {}
 *            tab_td_right_html
 * @param {}
 *            $tab_tr
 * @param {}
 *            $bgta_tab
 */
GoodsFormHandler.createFormTable = function($tab_td_left, tab_td_left_html,
		$tab_td_right, tab_td_right_html, $tab_tr, $bgta_tab) {
	$tab_td_left.append(tab_td_left_html);
	$tab_td_right.append(tab_td_right_html);
	$tab_tr.append($tab_td_left);
	$tab_tr.append($tab_td_right);

	if ($bgta_tab != null) {
		$bgta_tab.append($tab_tr);
	}
}

/**
 * 对比数组中各个元素，返回增一后的最大索引
 * 
 * @param {}
 *            s
 * @return {}
 */
function bijiao(s) {
	var z = s[1];
	for (var i = 2; i < s.length; i++) {
		if (parseInt(z) < parseInt(s[i]))
			z = s[i];
	}

	var zq = parseInt(z) + 1;
	return zq;
}

/**
 * 添加新行
 * 
 * @param {}
 *            btaName
 * @param {}
 *            btaMode
 * @param {}
 *            cobData
 * @param {}
 *            eventTag
 */
GoodsFormHandler.addNewRow = function(btaName, btaMode, cobData, eventTag) {
	var ksl = eventTag;
	
	for (var i = 0; i < dynamicIdsArray.length; i++) {
		if (dynamicIdsArray[i][0] == eventTag) {
			var x = bijiao(dynamicIdsArray[i]);
			ksl += "_" + x;
			var z = dynamicIdsArray[i];
			z[z.length] = x;
			
			dynamicIdsArray.remove(dynamicIdsArray[i]);
			dynamicIdsArray.push(z);
			
			break;
		}
	}

	var $bgta_tab = $("#typeAttrListTab");
	var $tab_tr = $("<tr id='" + ksl + "'></tr>");
	var $tab_td_left = $("<td align='right' width='15px;'></td>");
	var $tab_td_right = $("<td></td>");
	var tab_td_left_html = "<a style='font-weight:bold;cursor: "
			+ "pointer;'onclick='GoodsFormHandler.delNewRow(\"" + ksl
			+ "\")'>[─]</a> " + btaName + "：";
	var tab_td_right_html = "";

	if (btaMode == 0) {
		tab_td_right_html = GoodsFormHandler.createMode1RHtml(btaMode, ksl);
	} else if (btaMode == 1) {
		tab_td_right_html = GoodsFormHandler.createMode1RHtml(btaMode, ksl);
	} else if (btaMode == 2) {
		tab_td_right_html = GoodsFormHandler.createMode1RHtml(btaMode, ksl);
	}

	GoodsFormHandler.createFormTable($tab_td_left, tab_td_left_html,
			$tab_td_right, tab_td_right_html, $tab_tr, null);

	var $tag = $("#" + eventTag);
	var trSeq = $tag.parent().parent().index();
	var $tag_tr = $tag.parent().parent();
	$tag_tr.after($tab_tr);

	mini.parse();

	if (mini.get(ksl + "0") != null && cobData != null && cobData != "null"
			&& btaMode == 2) {
		mini.get(ksl + "0").setData(cobData);
	}
}

/**
 * 删除数组中指定的元素后返回该数组
 * 
 * @param {}
 *            array
 * @param {}
 *            s
 * @return {}
 */
function removeArrayElement(array, s) {
	for (var i = 1; i < array.length; i++) {
		if (parseInt(array[i]) == parseInt(s))
			array.remove(array[i]);
	}
	
	return array;
}

/**
 * 删除行
 * 
 * @param {}
 *            index
 */
GoodsFormHandler.delNewRow = function(index) {
	$("#typeAttrListTab tr[id=\'" + index + "\']").remove();
	
	var st = index.split("_");
	for (var i = 0; i < dynamicIdsArray.length; i++) {
		if (dynamicIdsArray[i][0] == st[0]) {
			var zq = removeArrayElement(dynamicIdsArray[i], st[1]);
			dynamicIdsArray.remove(dynamicIdsArray[i]);
			dynamicIdsArray.push(zq);
			break;
		}
	}

}

/**
 * 根据商品编号从商品属性表中获取指定的信息
 * 
 * @param {}
 *            bgaGoods
 */
GoodsFormHandler.getGoodsAttrTab = function(bgiId) {
	$.ajax({
		url : __root + "/getGoodsAttrTab.do?bgiId=" + bgiId,
		cache : false,
		dataType : 'json',
		success : function(text) {
			var $bgta_tab = $("#typeAttrListTab");
			$bgta_tab.empty();
			dynamicIdsArray.length = 0; // 清空数组
			staticIdsStr = "";          // 清空静态id字符串

			if (text != null && text != "" && text != "null") {
				// 把属性保存在全局变量中
				for (var m = 0, n = text.length; m < n; m++) {
					var obj = {
						bgaId : text[m][4],
						bgaGoods : text[m][5],
						bgaGtype : text[m][6],
						bgaTattri : text[m][7],
						bgaValue : text[m][8],
						bgaPrice : text[m][9],
						bgaOrder : text[m][10]
					};
					GOODSATTR.push(obj);
				}

				for (var i = 0; i < text.length; i++) {
					// 设置商品所属类型
					if (i == 0) {
						mini.get("btaGtype_gft").setValue(text[0][6]);
						gBshoptype = text[0][6];
					}

					// 单条记录
					var ls = text[i];

					// 新增商品类型属性时，商品属性表中的类型属性为空，需将新添加的属性id填充至此。
					if (ls[7] == null || ls[7] == "") {
						ls[7] = ls[11];
					}

					// 判断是否是相同属性的不同值
					if (parseInt(ls[0]) != 0) {
						var z = [ls[7], 0]; // 累加动态id字符串的值
						if (dynamicIdsArray.length > 0) {
							if (dynamicIdsArray[dynamicIdsArray.length - 1][0] == ls[7]) {
								dynamicIdsArray[dynamicIdsArray.length - 1]
										.push(counts);
							} else {
								dynamicIdsArray[dynamicIdsArray.length] = z;
								counts = 0;
								shopTypeAttribute = "";
							}
						} else {
							dynamicIdsArray[dynamicIdsArray.length] = z;
							counts = 0;
							shopTypeAttribute = "";
						}
					}

					var zt = ls[7] + "_" + counts;
					var $tab_tr = $("<tr id=" + zt + "></tr>");
					var $tab_td_left = $("<td align='right' width='130px;'></td>");
					var $tab_td_right = $("<td></td>");
					var tab_td_left_html = ls[2] + "：";
					var tab_td_right_html = "";
					var comboxData = [GoodsFormHandler.commonWarningData];
					var optionData = []; // 列表数组

					if (parseInt(ls[0]) == 0) {
						// 累加静态id字符串的值
						staticIdsStr += ls[7] + "=";
						if (typeof(ls[1]) == "number") {
							if (parseInt(ls[1]) == 0) {
								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							} else if (parseInt(ls[1]) == 1) {
								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							} else if (parseInt(ls[1]) == 2) {
								if (ls[3] != null && ls[3] != ""
										&& ls[3].length > 0) {
									optionData = ls[3].split("，");
									var j = 0;
									for (; j < optionData.length; j++) {
										comboxData[j + 1] = {
											id : optionData[j],
											text : optionData[j]
										};
									}
									comboxData.length = j + 1;
								}

								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							}
							GoodsFormHandler.createFormTable($tab_td_left,
									tab_td_left_html, $tab_td_right,
									tab_td_right_html, $tab_tr, $bgta_tab);
							mini.parse();

							if (mini.get(ls[7]) != null && ls[1] == 2) {
								mini.get(ls[7]).setData(comboxData);
							}

							if (mini.get(ls[7]) != null)
								mini.get(ls[7]).setValue(ls[8]);
						}
					} else {
						if (typeof(ls[1]) == "number") {
							if (ls[1] == 0) {
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							} else if (ls[1] == 1) {
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							} else if (ls[1] == 2) {
								if (ls[3] != null && ls[3] != ""
										&& ls[3].length > 0) {
									optionData = ls[3].split("，");
									var j = 0;
									for (; j < optionData.length; j++) {
										comboxData[j + 1] = {
											id : optionData[j],
											text : optionData[j]
										};
									}
									comboxData.length = j + 1;
								}
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							}

							if (shopTypeAttribute == ""
									|| shopTypeAttribute != ls[2])
								tab_td_left_html = GoodsFormHandler
										.createMode1LHtml(ls[7], ls[2], ls[1],
												comboxData);
							else {
								tab_td_left_html = GoodsFormHandler
										.createMode1Reduce(ls[7], ls[2], ls[1],
												comboxData, counts);
							}

							GoodsFormHandler.createFormTable($tab_td_left,
									tab_td_left_html, $tab_td_right,
									tab_td_right_html, $tab_tr, $bgta_tab);
							if (shopTypeAttribute == "")
								shopTypeAttribute = ls[2];
							mini.parse();

							// 设置下拉列表选项内容
							if (mini.get(ls[7] + "_" + counts + "0") != null
									&& ls[1] == 2) {
								mini.get(ls[7] + "_" + counts + "0")
										.setData(comboxData);
							}

							// 设置对应选项选择的值
							if (mini.get(ls[7] + "_" + counts + "0") != null)
								mini.get(ls[7] + "_" + counts + "0")
										.setValue(ls[8]);
							if (mini.get(ls[7] + "_" + counts + "1") != null)
								mini.get(ls[7] + "_" + counts + "1")
										.setValue(ls[9]);
							if (mini.get(ls[7] + "_" + counts + "2") != null)
								mini.get(ls[7] + "_" + counts + "2")
										.setValue(ls[10]);
							counts += 1;
						}
					}
				}
			}
		}
	});
}

/**
 * 创建属性列表表单
 * 
 * btaGtype_gft : 商品类型
 * 
 * @param {}
 *            e
 */
GoodsFormHandler.createGoodsAttrTab = function(e) {
	var goodsType = e.value;

	dynamicIdsArray.length = 0; // 清空数组
	staticIdsStr = "";          // 清空静态id字符串

	if (gBshoptype != null && gBshoptype == goodsType) {
		// GoodsFormHandler.getGoodsAttrByBgiId(gBgiId);
		// 加载最初绘制的商品属性列表表格
		GoodsFormHandler.getGoodsAttrTab(gBgiId);
	} else {
		$.ajax({
			url : __root + "/getAllGoodsTypeAttr.do?btaGtype=" + goodsType,
			cache : false,
			dataType : 'json',
			success : function(text) {
				var $bgta_tab = $("#typeAttrListTab");
				$bgta_tab.empty();

				for (var i = 0; i < text.length; i++) {
					var btaId = text[i].btaId;
					var btaName = text[i].btaName;
					var btaAtype = text[i].btaAtype;
					var btaMode = text[i].btaMode;
					var btaValue = text[i].btaValue;

					var $tab_tr = $("<tr></tr>");
					var $tab_td_left = $("<td align='right' width='130px;'></td>");
					var $tab_td_right = $("<td></td>");
					var tab_td_left_html = btaName + "：";
					var tab_td_right_html = "";

					// 列表数组
					var optionData = [];
					// 组合列表数组
					var comboxData = [GoodsFormHandler.commonWarningData];

					if (typeof(btaAtype) == "number") {
						// 唯一属性(一共三个：唯一属性、单选属性、复选属性)
						if (btaAtype == 0) {
							// 累加静态id字符串的值
							staticIdsStr += btaId + "=";

							if (typeof(btaMode) == "number") {
								if (btaMode == 0) {
									tab_td_right_html = GoodsFormHandler
											.createMode0RHtml(btaMode, btaId);

								} else if (btaMode == 1) {
									tab_td_right_html = GoodsFormHandler
											.createMode0RHtml(btaMode, btaId);
								} else if (btaMode == 2) {
									if (btaValue != null && btaValue != ""
											&& btaValue.length > 0) {
										optionData = btaValue.split("，");
										for (var j = 0; j < optionData.length; j++) {
											comboxData[j + 1] = {
												id : optionData[j],
												text : optionData[j]
											};
										}
									}

									tab_td_right_html = GoodsFormHandler
											.createMode0RHtml(btaMode, btaId);
								} else {
									tab_td_right_html = GoodsFormHandler
											.createMode0RHtml(null, null, null);
								}

								GoodsFormHandler.createFormTable($tab_td_left,
										tab_td_left_html, $tab_td_right,
										tab_td_right_html, $tab_tr, $bgta_tab);

								mini.parse();

								if (mini.get(btaId) != null && btaMode == 2) {
									mini.get(btaId).setData(comboxData);
								}

							}
						} else if (btaAtype == 1) { // 单选属性
							// 累加动态id字符串的值
							var z = [btaId, 0];

							dynamicIdsArray[dynamicIdsArray.length] = z;

							if (typeof(btaMode) == "number") {
								if (btaMode == 0) {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else if (btaMode == 1) {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else if (btaMode == 2) {
									if (btaValue != null && btaValue != ""
											&& btaValue.length > 0) {
										optionData = btaValue.split("，");
										for (var j = 0; j < optionData.length; j++) {
											comboxData[j + 1] = {
												id : optionData[j],
												text : optionData[j]
											};
										}
									}

									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(null, null, null,
													null, null, null, null);
								}

								tab_td_left_html = GoodsFormHandler
										.createMode1LHtml(btaId, btaName,
												btaMode, comboxData);

								GoodsFormHandler.createFormTable($tab_td_left,
										tab_td_left_html, $tab_td_right,
										tab_td_right_html, $tab_tr, $bgta_tab);

								mini.parse();

								if (mini.get(btaId + "_00") != null
										&& btaMode == 2) {
									mini.get(btaId + "_00").setData(comboxData);
								}
							}
						} else if (btaAtype == 2) { // 复选属性
							// 累加动态id字符串的值
							var z = [btaId, 0];
							dynamicIdsArray[dynamicIdsArray.length] = z;

							if (typeof(btaMode) == "number") {

								if (btaMode == 0) {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else if (btaMode == 1) {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else if (btaMode == 2) {
									if (btaValue != null && btaValue != ""
											&& btaValue.length > 0) {
										optionData = btaValue.split("，");
										for (var j = 0; j < optionData.length; j++) {
											comboxData[j + 1] = {
												id : optionData[j],
												text : optionData[j]
											};
										}
									}

									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(btaMode, btaId
															+ "_0");
								} else {
									tab_td_right_html = GoodsFormHandler
											.createMode1RHtml(null, null, null,
													null, null, null, null);
								}

								tab_td_left_html = GoodsFormHandler
										.createMode1LHtml(btaId, btaName,
												btaMode, comboxData);

								GoodsFormHandler.createFormTable($tab_td_left,
										tab_td_left_html, $tab_td_right,
										tab_td_right_html, $tab_tr, $bgta_tab);

								mini.parse();

								if (mini.get(btaId + "_00") != null
										&& btaMode == 2) {
									mini.get(btaId + "_00").setData(comboxData);
								}
							}
						}
					}
				}
			}
		});
	}
}

/**
 * 设置商品类型下拉组合框
 * 
 * @param {}
 *            bggId 商品分组编号
 * @param {}
 *            isInit 是否初始状态:[true，false]
 */
GoodsFormHandler.setGoodsTypeCob = function(bggId, isInit) {
	var mini_btaGtype_gft = mini.get("btaGtype_gft");
	var $bgtWarningId = $("#bgtWarningId");

	$.ajax({
				url : __root + "/getBgtByBggId.do?bggId=" + bggId,
				cache : false,
				dataType : 'json',
				success : function(text) {
					if (text != null && text != '' && text != '[]') {
						mini_btaGtype_gft.enable();
						mini_btaGtype_gft.setData(text);
						$bgtWarningId.html("请选择商品类型以完善此商品的属性！");

						if (!isInit) {
							if (gBggId != null && gBggId != ""
									&& bggId == gBggId) {
								if (gBgiId != null && gBgiId != "") {
									// 加载最初绘制的商品属性列表表格
									GoodsFormHandler.getGoodsAttrTab(gBgiId);
								}
							} else {
								mini_btaGtype_gft.select(0);
							}
						}
					} else {
						mini_btaGtype_gft.disable();
						$bgtWarningId.html("尚未配置商品分类属性！");
					}
				}
			});
}

/**
 * 标准方法接口定义
 * 
 * @param {}
 *            data
 */
GoodsFormHandler.passData = function(data) {
	if (data.action == "edit") {
		// 跨页面传递的数据对象，克隆后才可以安全使用
		data = mini.clone(data);
		GoodsFormHandler.formData.bgiId = data.bgiId;

		// 根据id获取对应的商品信息
		$.ajax({
			url : __root + "/getGoodsInfoById.do?bgiId=" + data.bgiId,
			cache : false,
			dataType : 'json',
			success : function(text) {
				// 全局的商品编号
				gBgiId = text.bgiId;
				// 全局的商品分组
				gBggId = text.bgiGroup;

				GoodsFormHandler.goodsForm.setData(text);

				mini.get("bgiOn_gft").setValue(XMAppUtils.obj2DateTime(text.bgiOn));
				mini.get("bgiOff_gft").setValue(XMAppUtils.obj2DateTime(text.bgiOff));

				if (text.bgiFeature != null && text.bgiFeature != "") {
					bgiFeatureEditor.html(text.bgiFeature);
				}

				if (text.bgiDetail != null && text.bgiDetail != "") {
					bgiDetailEditor.html(text.bgiDetail)
				}

				// GoodsFormHandler.getGoodsAttrByBgiId(text.bgiId);
				// GoodsFormHandler.getGoodsAttrTab(text.bgiId);
				// GoodsFormHandler.setGoodsTypeCob(text.bgiGroup, true);
				GoodsFormHandler.printNamedBgaTab(text.bgiId, text.bgiGroup);

				$("#bgtWarningId").html("["
						+ mini.get("bgiGroup_gft").getText() + "]" + "的属性列表！");

			}
		});
	} else if (data.action == "new") {
		// 默认商品状态为有效
		mini.get("bgiState").setValue(1);
		// 设置商品类型下拉列表框为不可用
		// mini.get("btaGtype_gft").disable();
		// $("#bgtWarningId").html("请选择商品分类，以绘制商品属性列表！");
	}
}

/**
 * 商品分类值改变事件处理程序
 */
GoodsFormHandler.changeGoodsTypeCob = function(e) {
	$("#typeAttrListTab").empty();
	mini.get("btaGtype_gft").disable();
	mini.get("btaGtype_gft").setData(null);
	$("#bgtWarningId").html("请选择商品分类，以绘制商品属性列表！");

	if (e.value != "" && e.value != "-1") {
		// 加载商品类型下拉列表框
		GoodsFormHandler.setGoodsTypeCob(e.value, false);
	}
}

/**
 * 商品分类值改变事件处理程序，绘制商品属性表格
 */
GoodsFormHandler.changeBgaTab = function(e) {
	dynamicIdsArray.length = 0;     // 清空动态数组
	staticIdsStr = "";              // 清空静态id字符串
	$("#typeAttrListTab").empty();  // 清空已绘制的表格

	if (e.value != "" && e.value != "-1") {
		if (gBgiId != "" && gBggId != "" && e.value == gBggId) {
			GoodsFormHandler.printNamedBgaTab(gBgiId, gBggId);
		} else {
			$.ajax({
						url : __root + "/getBgaTabData.do?bggId=" + e.value,
						cache : false,
						dataType : 'json',
						success : function(text) {
							if (text != null && text != '' && text != '[]') {
								// 绘制商品属性表格
								GoodsFormHandler.printBgaTab(text);
							}
						}
					});
		}

		$("#bgtWarningId").html("[" + mini.get("bgiGroup_gft").getText() + "]" + "的属性列表！");
	} else {
		$("#bgtWarningId").html("请选择商品分类，以绘制商品属性列表！");
	}
}

/**
 * 绘制商品属性表格
 * 
 * @param {}
 *            data
 */
GoodsFormHandler.printBgaTab = function(data) {
	// 获得商品属性表格对象
	var $bgta_tab = $("#typeAttrListTab");

	for (var i = 0; i < data.length; i++) {
		var btaId = data[i].btaId;
		var btaName = data[i].btaName;
		var btaAtype = data[i].btaAtype;
		var btaMode = data[i].btaMode;
		var btaValue = data[i].btaValue;
		gbGoodsType = data[0].btaGtype;

		var $tab_tr = $("<tr></tr>");
		var $tab_td_left = $("<td align='right' width='130px;'></td>");
		var $tab_td_right = $("<td></td>");
		var tab_td_left_html = btaName + "：";
		var tab_td_right_html = "";

		// 列表数组
		var optionData = [];
		// 组合列表数组
		var comboxData = [GoodsFormHandler.commonWarningData];

		if (typeof(btaAtype) == "number") {
			// 唯一属性(一共三个：唯一属性、单选属性、复选属性)
			if (btaAtype == 0) {
				// 累加静态id字符串的值
				staticIdsStr += btaId + "=";

				if (typeof(btaMode) == "number") {
					if (btaMode == 0) {
						tab_td_right_html = GoodsFormHandler.createMode0RHtml(
								btaMode, btaId);

					} else if (btaMode == 1) {
						tab_td_right_html = GoodsFormHandler.createMode0RHtml(
								btaMode, btaId);
					} else if (btaMode == 2) {
						if (btaValue != null && btaValue != ""
								&& btaValue.length > 0) {
							optionData = btaValue.split("，");
							for (var j = 0; j < optionData.length; j++) {
								comboxData[j + 1] = {
									id : optionData[j],
									text : optionData[j]
								};
							}
						}

						tab_td_right_html = GoodsFormHandler.createMode0RHtml(
								btaMode, btaId);
					} else {
						tab_td_right_html = GoodsFormHandler.createMode0RHtml(
								null, null, null);
					}

					GoodsFormHandler.createFormTable($tab_td_left,
							tab_td_left_html, $tab_td_right, tab_td_right_html,
							$tab_tr, $bgta_tab);

					mini.parse();

					if (mini.get(btaId) != null && btaMode == 2) {
						mini.get(btaId).setData(comboxData);
					}

				}
			} else if (btaAtype == 1) { // 单选属性
				// 累加动态id字符串的值
				var z = [btaId, 0];

				dynamicIdsArray[dynamicIdsArray.length] = z;

				if (typeof(btaMode) == "number") {
					if (btaMode == 0) {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else if (btaMode == 1) {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else if (btaMode == 2) {
						if (btaValue != null && btaValue != ""
								&& btaValue.length > 0) {
							optionData = btaValue.split("，");
							for (var j = 0; j < optionData.length; j++) {
								comboxData[j + 1] = {
									id : optionData[j],
									text : optionData[j]
								};
							}
						}

						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								null, null, null, null, null, null, null);
					}

					tab_td_left_html = GoodsFormHandler.createMode1LHtml(btaId,
							btaName, btaMode, comboxData);

					GoodsFormHandler.createFormTable($tab_td_left,
							tab_td_left_html, $tab_td_right, tab_td_right_html,
							$tab_tr, $bgta_tab);

					mini.parse();

					if (mini.get(btaId + "_00") != null && btaMode == 2) {
						mini.get(btaId + "_00").setData(comboxData);
					}
				}
			} else if (btaAtype == 2) { // 复选属性
				// 累加动态id字符串的值
				var z = [btaId, 0];
				dynamicIdsArray[dynamicIdsArray.length] = z;

				if (typeof(btaMode) == "number") {

					if (btaMode == 0) {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else if (btaMode == 1) {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else if (btaMode == 2) {
						if (btaValue != null && btaValue != ""
								&& btaValue.length > 0) {
							optionData = btaValue.split("，");
							for (var j = 0; j < optionData.length; j++) {
								comboxData[j + 1] = {
									id : optionData[j],
									text : optionData[j]
								};
							}
						}

						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								btaMode, btaId + "_0");
					} else {
						tab_td_right_html = GoodsFormHandler.createMode1RHtml(
								null, null, null, null, null, null, null);
					}

					tab_td_left_html = GoodsFormHandler.createMode1LHtml(btaId,
							btaName, btaMode, comboxData);

					GoodsFormHandler.createFormTable($tab_td_left,
							tab_td_left_html, $tab_td_right, tab_td_right_html,
							$tab_tr, $bgta_tab);

					mini.parse();

					if (mini.get(btaId + "_00") != null && btaMode == 2) {
						mini.get(btaId + "_00").setData(comboxData);
					}
				}
			}
		}
	}
}

/**
 * 绘制指定商品的属性表格
 * 
 * @param {}
 *            bgiId
 * @param {}
 *            bgiGroup
 */
GoodsFormHandler.printNamedBgaTab = function(bgiId, bgiGroup) {
	$.ajax({
		url : __root + "/getBgaTabPrint.do",
		cache : false,
		data : {
			bgiId : bgiId,
			bgiGroup : bgiGroup
		},
		dataType : 'json',
		success : function(text) {
			var $bgta_tab = $("#typeAttrListTab");
			$bgta_tab.empty();          // 清空已绘制的表格
			dynamicIdsArray.length = 0; // 清空数组
			staticIdsStr = "";          // 清空静态id字符串

			if (text != null && text != "" && text != "null") {
				// 把属性保存在全局变量中
				for (var m = 0, n = text.length; m < n; m++) {
					var obj = {
						bgaId : text[m][4],
						bgaGoods : text[m][5],
						bgaGtype : text[m][6],
						bgaTattri : text[m][7],
						bgaValue : text[m][8],
						bgaPrice : text[m][9],
						bgaOrder : text[m][10]
					};
					GOODSATTR.push(obj);
				}

				for (var i = 0; i < text.length; i++) {
					// 设置商品所属类型
					if (i == 0) {
						// mini.get("btaGtype_gft").setValue(text[0][6]);
						gBshoptype = text[0][6];
					}

					// 单条记录
					var ls = text[i];

					// 新增商品类型属性时，商品属性表中的类型属性为空，需将新添加的属性id填充至此。
					if (ls[7] == null || ls[7] == "") {
						ls[7] = ls[11];
					}

					// 判断是否是相同属性的不同值
					if (parseInt(ls[0]) != 0) {
						var z = [ls[7], 0]; // 累加动态id字符串的值
						if (dynamicIdsArray.length > 0) {
							if (dynamicIdsArray[dynamicIdsArray.length - 1][0] == ls[7]) {
								dynamicIdsArray[dynamicIdsArray.length - 1]
										.push(counts);
							} else {
								dynamicIdsArray[dynamicIdsArray.length] = z;
								counts = 0;
								shopTypeAttribute = "";
							}
						} else {
							dynamicIdsArray[dynamicIdsArray.length] = z;
							counts = 0;
							shopTypeAttribute = "";
						}
					}

					var zt = ls[7] + "_" + counts;
					var $tab_tr = $("<tr id=" + zt + "></tr>");
					var $tab_td_left = $("<td align='right' width='130px;'></td>");
					var $tab_td_right = $("<td></td>");
					var tab_td_left_html = ls[2] + "：";
					var tab_td_right_html = "";
					var comboxData = [GoodsFormHandler.commonWarningData];
					var optionData = []; // 列表数组

					if (parseInt(ls[0]) == 0) {
						// 累加静态id字符串的值
						staticIdsStr += ls[7] + "=";
						if (typeof(ls[1]) == "number") {
							if (parseInt(ls[1]) == 0) {
								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							} else if (parseInt(ls[1]) == 1) {
								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							} else if (parseInt(ls[1]) == 2) {
								if (ls[3] != null && ls[3] != ""
										&& ls[3].length > 0) {
									optionData = ls[3].split("，");
									var j = 0;
									for (; j < optionData.length; j++) {
										comboxData[j + 1] = {
											id : optionData[j],
											text : optionData[j]
										};
									}
									comboxData.length = j + 1;
								}

								tab_td_right_html = GoodsFormHandler
										.createMode0RHtml(ls[1], ls[7]);
							}
							GoodsFormHandler.createFormTable($tab_td_left,
									tab_td_left_html, $tab_td_right,
									tab_td_right_html, $tab_tr, $bgta_tab);
							mini.parse();

							if (mini.get(ls[7]) != null && ls[1] == 2) {
								mini.get(ls[7]).setData(comboxData);
							}

							if (mini.get(ls[7]) != null)
								mini.get(ls[7]).setValue(ls[8]);
						}
					} else {
						if (typeof(ls[1]) == "number") {
							if (ls[1] == 0) {
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							} else if (ls[1] == 1) {
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							} else if (ls[1] == 2) {
								if (ls[3] != null && ls[3] != ""
										&& ls[3].length > 0) {
									optionData = ls[3].split("，");
									var j = 0;
									for (; j < optionData.length; j++) {
										comboxData[j + 1] = {
											id : optionData[j],
											text : optionData[j]
										};
									}
									comboxData.length = j + 1;
								}
								tab_td_right_html = GoodsFormHandler
										.createMode1RHtml(ls[1], ls[7] + "_"
														+ counts);
							}

							if (shopTypeAttribute == ""
									|| shopTypeAttribute != ls[2])
								tab_td_left_html = GoodsFormHandler
										.createMode1LHtml(ls[7], ls[2], ls[1],
												comboxData);
							else {
								tab_td_left_html = GoodsFormHandler
										.createMode1Reduce(ls[7], ls[2], ls[1],
												comboxData, counts);
							}

							GoodsFormHandler.createFormTable($tab_td_left,
									tab_td_left_html, $tab_td_right,
									tab_td_right_html, $tab_tr, $bgta_tab);
							if (shopTypeAttribute == "")
								shopTypeAttribute = ls[2];
							mini.parse();

							// 设置下拉列表选项内容
							if (mini.get(ls[7] + "_" + counts + "0") != null
									&& ls[1] == 2) {
								mini.get(ls[7] + "_" + counts + "0")
										.setData(comboxData);
							}

							// 设置对应选项选择的值
							if (mini.get(ls[7] + "_" + counts + "0") != null)
								mini.get(ls[7] + "_" + counts + "0")
										.setValue(ls[8]);
							if (mini.get(ls[7] + "_" + counts + "1") != null)
								mini.get(ls[7] + "_" + counts + "1")
										.setValue(ls[9]);
							if (mini.get(ls[7] + "_" + counts + "2") != null)
								mini.get(ls[7] + "_" + counts + "2")
										.setValue(ls[10]);
							counts += 1;
						}
					}
				}
			}
		}
	});
}

/**
 * 保存（新增或编辑）商品信息
 */
GoodsFormHandler.saveGoodsInfo = function() {
	var goodsFormData = GoodsFormHandler.goodsForm.getData(true);
	goodsFormData.bgiId = GoodsFormHandler.formData.bgiId;

	GoodsFormHandler.goodsForm.validate();
	if (GoodsFormHandler.goodsForm.isValid() == false)
		return;
	
	// 保存期间设置控件不可用
	mini.get("saveBgiBtn").disable();
	
	if (bgiFeatureEditor) {
		goodsFormData.bgiFeature = bgiFeatureEditor.html();
	}

	if (bgiDetailEditor) {
		goodsFormData.bgiDetail = bgiDetailEditor.html()
	}

	var json = mini.encode(goodsFormData);
	$.ajax({
				url : __root + "/saveOrUpdateGoodsInfo.do",
				type : "POST",
				data : {
					goodsInfo : json,
		    		lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
				},
				cache : false,
				dataType : 'text',
				success : function(bgiId) {
					if (bgiId != null && bgiId != "") {
						// var btaGtype = mini.get("btaGtype_gft").getValue();
						// GoodsFormHandler.saveGoodsAttr(bgiId, btaGtype);

						GoodsFormHandler.saveGoodsAttr(bgiId);
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					MiniuiTools.miniError("系统提示", jqXHR.responseText,
							function() {
								GoodsFormHandler.closeCurrentWin();
							});
				}
			});
}

/**
 * 保存（添加或编辑）商品属性
 * 
 * @param {}
 *            bgiId
 */
GoodsFormHandler.saveGoodsAttr = function(bgiId) {
	var dynamicBgaData = [];

	if (dynamicIdsArray != null && dynamicIdsArray != "") {
		// 获取页面最新属性的同时，过滤重复项。
		var t = 0;
		for (var i = 0; i < dynamicIdsArray.length; i++) {
			var za = dynamicIdsArray[i];
			var flag = false;

			for (var j = 1; j < za.length; j++) {
				var id0 = za[0] + "_" + za[j] + "0";
				var id1 = za[0] + "_" + za[j] + "1";
				var id2 = za[0] + "_" + za[j] + "2";

				var objData = {
					bgaId : "",
					bgaTattri : za[0],
					bgaValue : mini.get(id0).getValue(),
					bgaPrice : mini.get(id1).getValue(),
					bgaOrder : mini.get(id2).getValue()
				};

				for (var m = 0, n = dynamicBgaData.length; m < n; m++) {
					if (objData.bgaTattri == dynamicBgaData[m].bgaTattri
							&& objData.bgaValue == dynamicBgaData[m].bgaValue) {
						dynamicBgaData[m].bgaPrice = objData.bgaPrice;
						flag = true;
						break;
					}
				}

				if (!flag) {
					dynamicBgaData.push(objData);
				}

				t++;
			}
		}

		if (GOODSATTR.length > 0) {
			// 在查询得到的原有数据中获取bgaId
			for (var i = 0, j = dynamicBgaData.length; i < j; i++) {
				for (var m = 0, n = GOODSATTR.length; m < n; m++) {
					if (dynamicBgaData[i].bgaTattri == GOODSATTR[m].bgaTattri
							&& dynamicBgaData[i].bgaValue == GOODSATTR[m].bgaValue) {
						dynamicBgaData[i].bgaId = GOODSATTR[m].bgaId;
						break;
					}
				}
			}
		}
	}

	var staticBgaData = [];
	if (staticIdsStr != "") {
		var qt = staticIdsStr.split("=");
		for (var i = 0; i < qt.length - 1; i++) {
			staticBgaData[i] = {
				bgaTattri : qt[i],
				bgaValue : mini.get(qt[i]).getValue()
			};
		}
	}

	var json1 = mini.encode(dynamicBgaData);
	var json2 = mini.encode(staticBgaData);

	$.ajax({
				url : __root + "/saveGoodsAttr.do",
				type : "POST",
				data : {
					bgiId : bgiId,
					btaGtype : gbGoodsType,
					dynamicJson : json1,
					staticJson : json2,
					lslUser: gbOperateUser,
		    		lslAddress: gbOperateIp
				},
				cache : false,
				dataType : 'text',
				success : function(data) {
					MiniuiTools.miniInfo("系统提示", "保存成功！", function(action) {
								GoodsFormHandler.closeCurrentWin("save");
							});
				},
				error : function(jqXHR, textStatus, errorThrown) {
					MiniuiTools.miniError("系统提示", jqXHR.responseText,
							function() {
								GoodsFormHandler.closeCurrentWin();
							});
				},
				complete: function(){
					mini.get("saveBgiBtn").enable();
				}
			});
}

/**
 * 关闭窗口
 * 
 * @param {}
 *            action
 */
GoodsFormHandler.closeCurrentWin = function(action) {
	if (window.CloseOwnerWindow)
		window.CloseOwnerWindow(action);
	else
		window.close();
}

/**
 * 取消并关闭窗口
 */
GoodsFormHandler.closeMiniWin = function() {
	GoodsFormHandler.closeCurrentWin("cancel");
}

/**
 * 显示、隐藏提示
 * 
 * @param {}
 *            field
 */
function showNotice(field) {
	if (field == "noticeBgt") {
		$("#bgtWarningId").fadeToggle("fast");
	}
}